﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Drawing;


public partial class WebUserControl_ControlOpenClosedDate : System.Web.UI.UserControl
{
    ClassExecute.ClassExecute dt;
    string str = BCDataCenter.Configuration.CurrentConnectionString;
    static string error = "";
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            try
            {
                GetTTPP();
                GetClosedDate(DropTTPP.SelectedValue.ToString());
            }
            catch (Exception ex)
            {
                BCDataCenter.ClassWriteLogErr obj = new BCDataCenter.ClassWriteLogErr();
                string method = System.Reflection.MethodBase.GetCurrentMethod().Name;
                obj.ErrorLog(ex.Message, method);
            }
        }
    }
    protected void btOpen_Click(object sender, EventArgs e)
    {
        try
        {
            dt = new ClassExecute.ClassExecute();
            int isCheckClosedMonth = dt.ExecuteSQl_int(str, "[ls_isClosedMonth]", CommandType.StoredProcedure, ref error,
                new SqlParameter("@selDate", txtNgayKhoaSo.Text),
                new SqlParameter("@siteCode", DropTTPP.SelectedValue.ToString()));
            if (isCheckClosedMonth == 0)
            {
                DateTime date;
                System.Globalization.DateTimeFormatInfo sFormat = new System.Globalization.DateTimeFormatInfo();
                sFormat.ShortDatePattern = "dd/MM/yyyy";
                date = Convert.ToDateTime(txtNgayKhoaSo.Text.Trim(), sFormat);

                bool kq = dt.ExecuteSQl_bool(str, CommandType.StoredProcedure, "[ls_openClosedDate]", ref error,
                    new SqlParameter("@selDate", date.AddDays(-1).ToString("dd/MM/yyyy")),
                    new SqlParameter("@siteCode", DropTTPP.SelectedValue.ToString()));
                if (kq)
                {
                    uscMsgBox1.AddMessage("Hoàn thành mở ngày khóa sổ", uscMsgBox.enmMessageType.Success);
                }
                else
                {
                    uscMsgBox1.AddMessage("Hệ thống đang quá tải,đề nghị bạn liên hệ Admin", uscMsgBox.enmMessageType.Error);
                }


            }
            else
            {
                uscMsgBox1.AddMessage("Ngày khóa sổ đã thuộc tháng đã khóa sổ", uscMsgBox.enmMessageType.Error);
            }
        }
        catch (Exception ex)
        {
            BCDataCenter.ClassWriteLogErr obj = new BCDataCenter.ClassWriteLogErr();
            string method = System.Reflection.MethodBase.GetCurrentMethod().Name;
            obj.ErrorLog(ex.Message, method);
        }
    }
    protected void DropTTPP_SelectedIndexChanged(object sender, EventArgs e)
    {
        try
        {
            GetClosedDate(DropTTPP.SelectedValue.ToString());
        }
        catch (Exception ex)
        {
            BCDataCenter.ClassWriteLogErr obj = new BCDataCenter.ClassWriteLogErr();
            string method = System.Reflection.MethodBase.GetCurrentMethod().Name;
            obj.ErrorLog(ex.Message, method);
        }

    }
    void GetClosedDate(string TTPP)
    {
        dt = new ClassExecute.ClassExecute();
        try
        {
            string closedDate = dt.GetDataDepend(str, "[ls_getClosedDate]", CommandType.StoredProcedure, ref error,
              new SqlParameter("@siteCode", TTPP));
            txtNgayKhoaSo.Text = closedDate;
             
          
        }
        catch
        {

        }
    }
    void GetTTPP()
    {
        dt = new ClassExecute.ClassExecute();
        DataTable db = dt.Get_Data_DataTable(str, ref error, "[ls_getDistributorList]", CommandType.StoredProcedure);
        DropTTPP.DataSource = db;
        DropTTPP.DataTextField = "sitename";
        DropTTPP.DataValueField = "Sitecode";
        DropTTPP.DataBind();
    }
}
